package com.ycsd.invest.order.business.msg;

import com.ycsd.invest.order.common.utils.EmailUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.workflow.engine.msg.AbstractMessageServiceHandler;
import kd.bos.workflow.engine.msg.ctx.MessageContext;
import kd.bos.workflow.engine.msg.info.MessageInfo;
import kd.bos.workflow.engine.msg.info.ToDoInfo;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @Descrіption: 消息渠道
 * @Author: yn
 * @Date: 2025/6/24 11:14
 */
public class TestSendMsg extends AbstractMessageServiceHandler {

    /**
     * @Author yn
     * @Description 工作流中的待办任务创建之后，会调用此接口，将待办任务传递给响应的渠道做处理
     * @Date 2025/6/24
     * @Param [messageContext, toDoInfo]
     * @return void
     **/
    @Override
    public void createToDo(MessageContext messageContext, ToDoInfo toDoInfo) {

    }

    /**
     * @Author yn
     * @Description 工作流中将待办变成已办的时候，会调用此接口进行待办变已办的处理
     * @Date 2025/6/24
     * @Param [messageContext, toDoInfo]
     * @return void
     **/
    @Override
    public void dealToDo(MessageContext messageContext, ToDoInfo toDoInfo) {

    }

    /**
     * @Author yn
     * @Description 工作流中撤回，或者多人收到任务，其中一人审批，删除其他人的待办时，调用此接口
     * @Date 2025/6/24
     * @Param [messageContext, toDoInfo]
     * @return void
     **/
    @Override
    public void deleteToDo(MessageContext messageContext, ToDoInfo toDoInfo) {

    }

    /**
     * @Author yn
     * @Description 发送预警，通知，报警等消息时，调用此接口进行当前渠道处理
     * @Date 2025/6/24
     * @Param [ctx, message]
     **/
    @Override
    public void sendMessage(MessageContext ctx, MessageInfo message) {
        System.out.println(message);
        System.out.println(this.getConfig());
        System.out.println(ctx);
        List<Long> currUserId = message.getUserIds();
        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load("bos_user", "email",
                new QFilter[]{new QFilter("id", QCP.in, currUserId)});
        List<String> emailList = new ArrayList<>();
        for (DynamicObject dynamicObject : dynamicObjects) {
            emailList.add(dynamicObject.getString("email"));
        }
        EmailUtil.sendHtmlMail(emailList, message.getTitle(), message.getContent());
        System.out.println(emailList);

    }
}
